﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionePolveri2.DAL;
using GestionePolveriLib.BL;
using FrameworkLib.Facades;
using System.Data.Objects;
using FrameworkLib.Filters;
using LinqKit;
using GestionePolveriLib.BL.FilterBags;

namespace GestionePolveriLib.Facades
{
    [Serializable]
    public class POSTableFacade : FacadeBase<GestionePolveriEntities>
    {
        public POSTableFacade()
        {

        }

        public void LoadAsync(BackgroundWorkerCallbackBase.Callback callback)
        {
            this.RunAsync(() => Get(), callback);
        }

        public List<GestionePolveriLib.BL.POS> Get()
        {
            List<GestionePolveriLib.BL.POS> retValue = null;

            try
            {
                var obj = from x in this.GetContext().POSTable
                          select new GestionePolveriLib.BL.POS
                          {
                              ID = x.IDPOSTable,
                              IDImpianto = x.IDImpianto,
                              IDOperatore = x.IDOperatore,
                              IDTurno = x.IDTurno,
                              POSValue = x.POS,
                              DateOfPos = x.DatePOS
                          };

                if (obj.Any())
                {
                    retValue = obj.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return retValue;
        }

        public List<GestionePolveriLib.BL.POS> GetPOSData()
        {
            List<GestionePolveriLib.BL.POS> retValue = null;

            GestionePolveriEntities m_ctx = this.GetContext();

            try
            {
                var obj = from x in m_ctx.POSTable
                          orderby x.DatePOS, x.Turni.Descrizione, x.Impianti.Impianto descending
                          select new GestionePolveriLib.BL.POS
                          {
                              ID = x.IDPOSTable,
                              IDImpianto = x.IDImpianto,
                              IDOperatore = x.IDOperatore,
                              IDTurno = x.IDTurno,
                              POSValue = x.POS,
                              DateOfPos = EntityFunctions.TruncateTime(x.DatePOS).Value,
                              Impianto = x.Impianti.Impianto,
                              Operatore = (from o in m_ctx.aspnet_Users where o.UserId == x.IDOperatore select o.UserName).FirstOrDefault(),
                              Turno = x.Turni.Descrizione
                          };

                if (obj.Any())
                {
                    retValue = obj.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return retValue;
        }

        public List<GestionePolveriLib.BL.POS> GetPOSData(FilterBase<POSTable> filter)
        {
            List<GestionePolveriLib.BL.POS> retValue = null;

            GestionePolveriEntities m_ctx = this.GetContext();

            Guid m_idOperatore = ((AnagraficaPOSFilterBag)filter).IDOperatore;

            try
            {
                var obj = from x in m_ctx.POSTable.AsExpandable().Where(filter.SetUpFilter())
                          join u in m_ctx.aspnet_Users on x.IDOperatore equals u.UserId
                          where u.UserId == m_idOperatore
                          orderby x.DatePOS, x.Turni.Descrizione, x.Impianti.Impianto descending
                          select new GestionePolveriLib.BL.POS
                          {
                              ID = x.IDPOSTable,
                              IDImpianto = x.IDImpianto,
                              IDOperatore = x.IDOperatore,
                              IDTurno = x.IDTurno,
                              POSValue = x.POS,
                              DateOfPos = EntityFunctions.TruncateTime(x.DatePOS).Value,
                              Impianto = x.Impianti.Impianto,
                              //Operatore = (from o in m_ctx.aspnet_Users where o.UserId == x.IDOperatore select o.UserName).FirstOrDefault(),
                              Operatore = u.UserName,
                              Turno = x.Turni.Descrizione
                          };

                if (obj.Any())
                {
                    retValue = obj.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return retValue;
        }

        public bool Add(GestionePolveriLib.BL.POS pos)
        {
            throw new NotImplementedException();
        }

        public bool Delete(GestionePolveriLib.BL.POS pos)
        {
            throw new NotImplementedException();
        }

        public bool Modify(GestionePolveriLib.BL.POS pos)
        {
            bool retValue = false;

            try
            {
                GestionePolveriEntities m_ctx = this.GetContext();

                var obj = from x in m_ctx.POSTable
                          where x.IDPOSTable == pos.ID
                          select x;

                if (obj.Any())
                {
                    POSTable f = obj.FirstOrDefault();

                    f.POS = pos.POSValue;

                    m_ctx.SaveChanges();

                    retValue = true;
                }
            }
            catch (Exception ex)
            {
                this.LastExceptionOccurred = ex;
                throw ex;
            }

            return retValue;
        }

        public POS GetCurrentPOS(Turno turno, Operatore operatore, Impianto impianto, DateTime data)
        {
            POS retValue = null;

            try
            {
                var obj = from x in this.GetContext().POSTable
                          where x.IDTurno == turno.Id
                          //&& x.IDOperatore == operatore.ID
                          && x.IDImpianto == impianto.ID
                          && (EntityFunctions.TruncateTime(x.DatePOS) == EntityFunctions.TruncateTime(data)) //DateTime.Now))
                          select new POS
                          {
                              POSValue = x.POS,
                              DateOfPos = x.DatePOS,
                              IDTurno = x.IDTurno,
                              IDOperatore = x.IDOperatore,
                              IDImpianto = x.IDImpianto
                          };

                if (obj.Any())
                {
                    retValue = obj.FirstOrDefault();

                    retValue.POSValue++;
                }
                else
                {
                    if ((turno.Id != -1) && (impianto.ID != -1))
                    {
                        retValue = new POS();
                        retValue.POSValue = 1;
                        retValue.DateOfPos = data; // DateTime.Now;
                        retValue.IDTurno = turno.Id;
                        retValue.IDOperatore = operatore.ID;
                        retValue.IDImpianto = impianto.ID;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw ex;
            }

            //UpdatePOS(turno, operatore, impianto, retValue, data);

            return retValue;
        }

        public bool UpdatePOS(Turno turno, Operatore operatore, Impianto impianto, POS newValue, DateTime data)
        {
            bool retValue = false;

            try
            {
                var obj = from x in this.GetContext().POSTable
                          where x.IDTurno == turno.Id
                          //&& x.IDOperatore == operatore.ID
                          && x.IDImpianto == impianto.ID
                          && (EntityFunctions.TruncateTime(x.DatePOS) == EntityFunctions.TruncateTime(data))
                          select x;

                if (obj.Any())
                {
                    GestionePolveri2.DAL.POSTable p = obj.FirstOrDefault();
                    p.POS = newValue.POSValue;
                    p.DatePOS = data; // DateTime.Now;
                }
                else
                {
                    GestionePolveri2.DAL.POSTable p = new POSTable();
                    p.IDTurno = turno.Id;
                    p.IDOperatore = operatore.ID;
                    p.IDImpianto = impianto.ID;
                    p.POS = 1;
                    p.DatePOS = data; // DateTime.Now;

                    this.GetContext().POSTable.AddObject(p);
                }

                this.GetContext().SaveChanges();

                retValue = true;
            }
            catch (Exception ex)
            {
                throw;
            }

            return retValue;
        }

        public bool CheckDuplicate(int pos, short turno, int impianto, DateTime data)
        {
            bool retValue = false;

            try
            {
                var obj = from x in this.GetContext().Movimenti
                          where x.IDTurno == turno
                          && x.IDImpianto == impianto
                          && (EntityFunctions.TruncateTime(x.DataMovimento) == EntityFunctions.TruncateTime(data))
                          && x.POS == pos
                          select x;

                if (obj.Any())
                {
                    retValue = true;
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            return retValue;
        }
    }
}
